package com.jinzecloud.reportapproval.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.jinzecloud.reportapproval.bean.*;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.service.KeyInstrumentDetailService;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.service.KeyInstrumentService;

import javax.servlet.http.HttpServletRequest;


/**
 * 主要仪器仪表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 16:18:45
 */
@RestController
@RequestMapping("reportapproval/keyinstrument")
public class KeyInstrumentController {
    @Autowired
    private KeyInstrumentService keyInstrumentService;

    @Autowired
    private KeyInstrumentDetailService keyInstrumentDetailService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:keyinstrument:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = keyInstrumentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:keyinstrument:info")
    public R info(@PathVariable("id") Integer id){
		KeyInstrument keyInstrument = keyInstrumentService.getById(id);

        List<KeyInstrumentDetail> parentId = keyInstrumentDetailService.list(new QueryWrapper<KeyInstrumentDetail>().eq("parent_id", keyInstrument.getId()));

        keyInstrument.setDetailList(parentId);

        return R.ok().put("keyInstrument", keyInstrument);
    }


    @RequestMapping("/getInfo/{projectNumber}")
    //@RequiresPermissions("reportapproval:keyinstrument:info")
    public R getInfo(@PathVariable("projectNumber") String projectNumber){
        String info = keyInstrumentService.getInfo(projectNumber);

        return R.ok().put("keyInstrument", info);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:keyinstrument:save")
    public R save(@RequestBody KeyInstrument keyInstrument){
        List<KeyInstrument> keyInstruments = keyInstrumentService.list(new QueryWrapper<KeyInstrument>().eq("project_number", keyInstrument.getProjectNumber()));
        if (keyInstruments.size() > 0) return R.error("编号："+keyInstrument.getProjectNumber()+"已存在，请勿重复添加！");
        keyInstrument.setStatus(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        keyInstrument.setUserId(adminService.getIdByName(authentication.getName()));
		return keyInstrumentService.addKeyInstrument(keyInstrument);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:keyinstrument:update")
    public R update(@RequestBody KeyInstrument keyInstrument){
		return keyInstrumentService.updateKeyInstrument(keyInstrument);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    //@RequiresPermissions("reportapproval:keyinstrument:delete")
    public R delete(@RequestBody Integer[] ids){
		keyInstrumentService.removeByIds(Arrays.asList(ids));
        for (Integer id : ids) {
            List<KeyInstrumentDetail> parentId = keyInstrumentDetailService.list(new QueryWrapper<KeyInstrumentDetail>().eq("parent_id", id));
            if (parentId.size() > 0) {
                for (KeyInstrumentDetail keyInstrumentDetail : parentId) {
                    keyInstrumentDetailService.removeById(keyInstrumentDetail);
                }
            }
        }
        return R.ok();
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
       return keyInstrumentService.viewPdf(id);
    }

    @RequestMapping("/viewWord/{id}")
    public String viewWord(@PathVariable("id") Integer id){
        return keyInstrumentService.viewWord(id);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(keyInstrumentService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody KeyInstrument keyInstrument) {
        keyInstrumentService.updateById(new KeyInstrument().setId(keyInstrument.getId()).setStatus(StateEnum.CHECK_DETECT.getState()));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return keyInstrumentService.audit(auditParam);
    }


}
